package com.example.wtt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wtt.common.Result;
import com.example.wtt.controller.dto.CollectDTO;
import com.example.wtt.controller.dto.UpvoteDTO;
import com.example.wtt.entity.Dynamic;
import com.example.wtt.entity.User;
import com.example.wtt.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dynamic") // 统一给接口加前缀
public class DynamicController {
    @Autowired
    private DynamicService dynamicService;

    @GetMapping("/page/{pageNum}")
    public Result test(@PathVariable Integer pageNum){
        return Result.success(dynamicService.allDynamics(pageNum,4));
    }

//    @GetMapping("/page") // 接口路径：/user/page
//    public Result findPage(@RequestParam Integer pageNum, @RequestParam String title){
//        IPage<Dynamic> page = new Page<>(pageNum, 8);
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().like(!"".equals(username),User::getUsername, username);
//        queryWrapper.orderByDesc("id");
//        return Result.success(dynamicService.page(page, queryWrapper));
//
//    }

    @PostMapping("/publish")
    public Result publishDynamic(@RequestBody Object obj){
        return Result.success(dynamicService.publicDynamic(obj));
    }

    @PostMapping("/upvote")
    public Result upvote(@RequestBody UpvoteDTO upvoteDTO){
        Integer flag = upvoteDTO.getFlag();
        if(flag == 0)
            return Result.success(dynamicService.upvoteSum(upvoteDTO.getDynamicId()));
        else
            return Result.success(dynamicService.upvoteSub(upvoteDTO.getDynamicId()));
    }

    @PostMapping("/collect")
    public Result collect(@RequestBody CollectDTO collectDTO){
        Integer flag = collectDTO.getFlag();
        if(flag == 0)
            return Result.success(dynamicService.collectSum(collectDTO));
        else
            return Result.success(dynamicService.collectSub(collectDTO));
    }

    @PostMapping("/queryCollect")
    public Result queryCollect(@RequestBody CollectDTO collectDTO){
        return Result.success(dynamicService.queryCollect(collectDTO));
    }

    @GetMapping("/queryDynamic/{describe}")
    public Result queryDynamic(@PathVariable String describe){
        return Result.success(dynamicService.queryDynamic(describe));
    }

}
